extends Node2D
# 构建一个移动的组件，这样以后不同的敌人都可以挂上这个组件，然后设置不同的速度。
# 这就是抽象的思维。
@export var max_speed: int = 200
@export var acceleration:float  = 20

var velocity = Vector2.ZERO

func accelerate_to_player():
	var owner_node2d = owner as Node2D
	if owner_node2d == null: return 
	var player = get_tree().get_first_node_in_group("player") as Node2D
	if player == null:
		print("没有获取到玩家")
	var direction = (player.global_position - owner_node2d.global_position).normalized()
	accelerate_in_direction(direction)

func accelerate_in_direction(dirction: Vector2):
	var desired_velocity = dirction * max_speed
	velocity = velocity.lerp(desired_velocity, 1-exp(get_process_delta_time() * -acceleration))
	
func move(charactor_body: CharacterBody2D):
	charactor_body.velocity = velocity
	charactor_body.move_and_slide()
	# 因为moveslice 会影响velocity 所以这里需要重新赋值
	velocity = charactor_body.velocity
